﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;



namespace FBDMaker
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window

    {
        public List<FolderTreeInf> nodes;
        public List<FolderTreeInf> nodes2;
        public List<FolderTreeInf> nodes3;
       
        public MainWindow()
        {
            InitializeComponent();
            nodes = new List<FolderTreeInf>();
            nodes2 = new List<FolderTreeInf>();
            //nodes3 = new List<FolderTreeInf>();
            FolderTreeInf.GetTopItem(nodes);
            //FolderTreeInf.GetTopItem(nodes2);
            //FolderTreeInf.GetTopItem(nodes3);
            treeView1.ItemsSource = nodes;
            treeView2.ItemsSource = nodes2;
            //treeViewt.ItemsSource = nodes3;
        }

        private void treeView1_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)e.OriginalSource;
            (item.Header as FolderTreeInf).OpenNode();
           // (treeView1.SelectedItem as FolderTreeInf).OpenNode();
        }

        private void treeView2_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)e.OriginalSource;
            (item.Header as FolderTreeInf).OpenNode(false);
            // (treeView1.SelectedItem as FolderTreeInf).OpenNode();
        }

        private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            FolderTreeInf item = (FolderTreeInf)e.NewValue;
            treeView2.ItemsSource = null;
            nodes2.Clear();
            FolderTreeInf.GetListItem(nodes2, item.FPath);
            treeView2.ItemsSource = nodes2;
        }

        private void treeView2_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {

        }

        private void AddAvtor_Click(object sender, RoutedEventArgs e)
        {
           // ( (FolderTreeInf)treeView2.SelectedItem).LibInfo.Avtor.Add(new LibPerson());
            System.Windows.Controls.Button ClickButton = (System.Windows.Controls.Button)sender;
            ObservableCollection<LibPerson> listpers = (ObservableCollection<LibPerson>)ClickButton.DataContext;
            LibPerson pers = new LibPerson();
            listpers.Add(pers);
        }

        private void DelAvtor_Click(object sender, RoutedEventArgs e)
        {
            //((FolderTreeInf)treeView2.SelectedItem).LibInfo.Avtor.Remove((LibPerson)listAvtor.SelectedItem);
            System.Windows.Controls.Button ClickButton = (System.Windows.Controls.Button)sender;
            LibPerson pers = (LibPerson)ClickButton.Tag;
            ObservableCollection<LibPerson> listpers = (ObservableCollection<LibPerson>)ClickButton.DataContext;
            listpers.Remove(pers);
        }

        private void treeSeq_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {

        }

        private void addSeq_Click(object sender, RoutedEventArgs e)
        {
           // ((FolderTreeInf)treeView2.SelectedItem).LibInfo.BookSequence.Add(new LibSequence(null));
            System.Windows.Controls.Button ClickButton = (System.Windows.Controls.Button)sender;
            //LibSequence pers = (LibSequence)ClickButton.Tag==null?;
            ObservableCollection<LibSequence> listseq = ((LibSequence)ClickButton.Tag==null ? (ObservableCollection<LibSequence>)ClickButton.DataContext:((LibSequence)ClickButton.Tag).SubSequence ) ;
            LibSequence seq = new LibSequence((LibSequence)ClickButton.Tag);
            listseq.Add(seq);
        }

        //private void addSubSeq_Click(object sender, RoutedEventArgs e)
        //{
        //    ((LibSequence)treeSeq.SelectedItem).SubSequence.Add(new LibSequence(((LibSequence)treeSeq.SelectedItem)));
        //}

        private void delSeq_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.Button ClickButton = (System.Windows.Controls.Button)sender;
            if ((LibSequence)ClickButton.Tag != null)
            {
                
                ObservableCollection<LibSequence> listseq = (((LibSequence)ClickButton.Tag).Parent == null ? (ObservableCollection<LibSequence>)ClickButton.DataContext : ((LibSequence)ClickButton.Tag).Parent.SubSequence  );
                //LibSequence seq = new LibSequence((LibSequence)ClickButton.Tag);
                listseq.Add((LibSequence)ClickButton.Tag);
            }
            //if (((LibSequence)treeSeq.SelectedItem).Parent == null)
            //    ((FolderTreeInf)treeView2.SelectedItem).LibInfo.BookSequence.Remove(((LibSequence)treeSeq.SelectedItem));
            //else
            //    ((LibSequence)treeSeq.SelectedItem).Parent.SubSequence.Remove(((LibSequence)treeSeq.SelectedItem));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            GenreTree GenreTreeWin = new GenreTree(((FolderTreeInf)treeView2.SelectedItem).LibInfo._Theme);
            GenreTreeWin.ShowDialog();
            
        }

        private void DelPic_Click(object sender, RoutedEventArgs e)
        {
            ((FolderTreeInf)treeView2.SelectedItem).LibInfo.Cover.Clear();
        }

        private void AddPic_Click(object sender, RoutedEventArgs e)
        {

            System.Windows.Forms.OpenFileDialog opf = new System.Windows.Forms.OpenFileDialog();
            opf.Multiselect = false;
            opf.Filter = "All files (*.*)|*.*";
            if (opf.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
            {
                ((FolderTreeInf)treeView2.SelectedItem).LibInfo.Cover.FillFromFile(opf.FileName);
            }
        }

       
        //Template="{DynamicResource TreeViewControlTemplate1}" ItemTemplate="{DynamicResource DataTemplate1}"
        // <ColumnDefinition Width="Auto" MinWidth="19" />
    }

}
